package com.skychyn.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class SolarDBAdapter {

	// Database fields
	public static final String KEY_ROWID = "_id";
	public static final String KEY_DATE = "date";
	public static final String KEY_PRES = "pressure";
	public static final String KEY_TEMP = "temperature";
	private static final String DB_TABLE = "solardata";

	private String[] fields = { KEY_ROWID, KEY_DATE, KEY_PRES, KEY_TEMP };

	private Context context;
	private SQLiteDatabase db;
	private SolarDBHelper dbHelper;

	public SolarDBAdapter(Context context) {
		this.context = context;
	}

	public SolarDBAdapter open() throws SQLException {
		dbHelper = new SolarDBHelper(context);
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	/**
	 * Create a new solar data record.
	 * 
	 * If the record is successfully created return the new rowId for that note,
	 * otherwise return a -1 to indicate failure.
	 */

	public long createSolarData(String Date, int press, int temp) {

		ContentValues values = createContentValues(Date, press, temp);

		return db.insert(DB_TABLE, null, values);
	}

	/**
	 * UpCalendar solar data record
	 */

	public boolean updateSolarData(long rowId, String Date, int press, int temp) {

		ContentValues values = createContentValues(Date, press, temp);

		return db.update(DB_TABLE, values, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Deletes solar data record
	 * 
	 */
	public boolean deleteSolarDataByRowID(long rowId) {
		return db.delete(DB_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Deletes all solar data records
	 * 
	 */
	public boolean deleteAllSolarData() {
		return db.delete(DB_TABLE, "1", null) > 0;
	}

	/**
	 * Return a Cursor over the list of all solar data records in the database
	 * 
	 * @return Cursor over all notes
	 */

	public Cursor fetchAllSolarData() {
		return db.query(DB_TABLE, fields, null, null, null, null, null);
	}

	/**
	 * Return a Cursor positioned at the defined solar data record
	 * 
	 */
	public Cursor fetchSolarDataByRowId(long rowId) throws SQLException {
		Cursor mCursor = db.query(true, DB_TABLE, fields, KEY_ROWID + "="
				+ rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	/**
	 * Return a Cursor over all nodes of defined Calendar
	 * 
	 */
	public Cursor fetchSolarDataByDate(String Date) throws SQLException {

		String _Date = Date;

		Cursor mCursor = db.query(true, DB_TABLE, fields, KEY_DATE + "=\""
				+ _Date + "\"", null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	/**
	 * Create a structure to keep data record content values
	 * 
	 */
	private ContentValues createContentValues(String Date, int press, int temp) {

		String _date = Date;
		String _press = Integer.toString(press);
		String _temp = Integer.toString(temp);

		ContentValues values = new ContentValues();

		values.put(KEY_DATE, _date);
		values.put(KEY_PRES, _press);
		values.put(KEY_TEMP, _temp);
		return values;
	}
}